package com.lottery.service.pc28.wealthService.impl;

import com.lottery.dao.pc28.wealthDao.AccountDao;
import com.lottery.dao.pc28.wealthDao.AccountTransactionDao;
import com.lottery.dto.ResultDto;
import com.lottery.entity.Account;
import com.lottery.entity.AccountTransaction;
import com.lottery.entity.WechatUnifiedOrder;
import com.lottery.service.pc28.wealthService.RechargeService;
import com.lottery.utils.RandCodeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

@Service
public class RechargeServiceImpl implements RechargeService {

	@Resource
	public AccountDao accountDao;

	@Resource
	public AccountTransactionDao accountTransactionDao;

	/**
	 * 支付宝充值接口
	 * 
	 * @return ResultDto
	 */

	public ResultDto<String> AlipayRecharge(Map<String, String> map) {
		ResultDto<String> resultDto = new ResultDto<String>();
		String form;
		try {
//			AlipayTrade alipayTrade = new AlipayTrade();
//			form = alipayTrade.TradeAppPayRequest(map);
//			resultDto.setData(form);
//			resultDto.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultDto.setSuccess(false);
		}
		return resultDto;
	}

	/**
	 * 微信充值接口实现
	 */

	public ResultDto<TreeMap<String, String>> WechatRecharge(WechatUnifiedOrder wechatUnifiedOrder) {
		ResultDto<TreeMap<String, String>> resultDto = new ResultDto<TreeMap<String, String>>();
		WechatTrade wechatTrade = new WechatTrade();
		TreeMap<String, String> unifiedOrderRequest = wechatTrade.unifiedOrderRequest(wechatUnifiedOrder);
		if (unifiedOrderRequest != null) {
			resultDto.setData(unifiedOrderRequest);
			resultDto.setMessage("200");
			resultDto.setSuccess(true);
		} else {
			resultDto.setMessage("400");
			resultDto.setSuccess(false);
		}
		return resultDto;
	}

	/**
	 * 插入充值都得数据 user_id 用户id transaction_money 变动金额 transaction_type 变动类型
	 * money_sources 资金来源
	 * 
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean insertRechargeDemo(int userId, int transaction_money, int transaction_type, String money_sources)
			throws Exception {
		boolean b = false;
		try {
			Account account = accountDao.selectAccountByUserId(userId);
			// double account_funds = account.getAccountFunds();
			// account.setAccountFunds(account_funds + transaction_money);
			account.setAccountFunds(transaction_money);
			int updateNum = accountDao.addAccount(account);
			if (updateNum == 0) {
				// resultDto.setData("更新失败");
				// resultDto.setSuccess(false);
				return false;
			}

			int account_id = account.getId();
			AccountTransaction accountTransaction = new AccountTransaction();
			accountTransaction.setAccountId(account_id);
			accountTransaction.setTransactionMoney(transaction_money);
			accountTransaction.setTransactionType(transaction_type);
			accountTransaction.setMoneySources(money_sources);
			accountTransaction.setMoney(transaction_money);
			accountTransaction.setTransactionDate(new Date());

			accountTransaction.setOrderId(RandCodeUtil.getOrderId());
			accountTransaction.setState(1);
			accountTransactionDao.insertAccountTransaction(accountTransaction);
			b = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			b = false;
			throw new Exception("");
		}
		return b;
	}

}
